<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Repositories\BeanTransaction;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Illuminate\Support\Collection;

class BeanTransactionController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BeanTransaction('user'), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->disableCreateButton();
            $grid->disableRefreshButton();
            $grid->disableActions();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('transaction_type', '类型', [
                    1 => '充值',
                    2 => '消费',
                    3 => '退款',
                    4 => '挂售',
                    5 => '转到',
                ]);
            });
            $grid->column('user.name', '用户名');
            $grid->column('user.flag', '用户标签');
            $grid->column('user.telephone', '会员电话')->copyable();
            $grid->column('change_type', '变动类型')->using([
                1 => '增加',
                2 => '减少',
            ]);

            $grid->column('amount');
            $grid->column('balance');
            $grid->column('transaction_type', '来源类型')->using([
                1 => '充值',
                2 => '消费',
                3 => '退款',
                4 => '挂售',
                5 => '转到',
            ]);
            $grid->column('describe');
//            $grid->column('status');

            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->rows(function (Collection $rows) {
                $rows->each(function ($item) {
                    if ($item->change_type == '减少') {
                        $item->setAttributes(['class' => 'bg-success']);
                    } else {
                        $item->setAttributes(['class' => 'bg-danger']);
                    }
                });
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user.name', '用户名');
                $filter->equal('user.flag', '用户标签');
                $filter->equal('user.telephone', '用户电话');
                $filter->equal('change_type', '变动类型')->select([
                    1 => '增加',
                    2 => '减少',
                ]);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BeanTransaction(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('amount');
            $show->field('balance');
            $show->field('transaction_type');
            $show->field('describe');
            $show->field('status');
            $show->field('change_type');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BeanTransaction(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('amount');
            $form->text('balance');
            $form->text('transaction_type');
            $form->text('describe');
            $form->text('status');
            $form->text('change_type');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
